package com.innovations.inn.service.impl;

import com.innovations.common.utils.DateUtils;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.UserAttention;
import com.innovations.inn.mapper.UserAttentionMapper;
import com.innovations.inn.service.IUserAttentionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户关注信息Service业务层处理
 *
 * @author dp
 * @date 2024-08-12
 */
@Service
public class UserAttentionServiceImpl implements IUserAttentionService {
    @Autowired
    private UserAttentionMapper userAttentionMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询用户关注信息
     *
     * @param attentionId 用户关注信息主键
     * @return 用户关注信息
     */
    @Override
    public UserAttention selectUserAttentionByAttentionId(Long attentionId) {
        return userAttentionMapper.selectUserAttentionByAttentionId(attentionId);
    }

    /**
     * 查询用户关注信息列表
     *
     * @param userAttention 用户关注信息
     * @return 用户关注信息
     */
    @Override
    public List<UserAttention> selectUserAttentionList(UserAttention userAttention) {
        return userAttentionMapper.selectUserAttentionList(userAttention);
    }

    /**
     * 新增用户关注信息
     *
     * @param userAttention 用户关注信息
     * @return 结果
     */
    @Override
    public int insertUserAttention(UserAttention userAttention) {
        userAttention.setCreateTime(DateUtils.getNowDate());
        return userAttentionMapper.insertUserAttention(userAttention);
    }

    /**
     * 修改用户关注信息
     *
     * @param userAttention 用户关注信息
     * @return 结果
     */
    @Override
    public int updateUserAttention(UserAttention userAttention) {
        if (userAttention.getModifyStatus().equals(1)) {
            // 新增
            return userAttentionMapper.insertUserAttention(userAttention);
        } else {
            // 修改
            return userAttentionMapper.updateUserAttention(userAttention);
        }
    }

    /**
     * 批量删除用户关注信息
     *
     * @param attentionIds 需要删除的用户关注信息主键
     * @return 结果
     */
    @Override
    public int deleteUserAttentionByAttentionIds(Long[] attentionIds) {
        return userAttentionMapper.deleteUserAttentionByAttentionIds(attentionIds);
    }

    /**
     * 删除用户关注信息信息
     *
     * @param attentionId 用户关注信息主键
     * @return 结果
     */
    @Override
    public int deleteUserAttentionByAttentionId(Long attentionId) {
        return userAttentionMapper.deleteUserAttentionByAttentionId(attentionId);
    }

    /**
     * 实体数据补全
     *
     * @param userAttention 实体
     * @param userId        用户id
     * @param crateType     创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(UserAttention userAttention, Long userId, Integer crateType) {
        // 查询关注信息
        UserAttention userAttentionSelect = new UserAttention();
        userAttentionSelect.setUserId(userId);
        userAttentionSelect.setAttentionItemId(userAttention.getAttentionItemId());
        userAttentionSelect.setAttentionType(userAttention.getAttentionType());
        List<UserAttention> attentionList = userAttentionMapper.selectUserAttentionList(userAttentionSelect);
        // 赋值关注id
        if (attentionList.isEmpty()) {
            userAttention.setModifyStatus(1);
            userAttention.setAttentionId(idGeneratorSnowflake.nextId());
        } else {
            userAttention.setModifyStatus(2);
            userAttention.setAttentionId(attentionList.get(0).getAttentionId());
        }
        // 赋值用户id
        userAttention.setUserId(userId);
        // 判断是否关注
        if (userAttention.getAttentionFlag().equals(1)) {
            // 赋值关注日期
            userAttention.setCreateTime(DateUtils.getNowDate());
            userAttention.setCancelTime(null);
        } else {
            // 赋值取消关注日期
            userAttention.setCancelTime(DateUtils.getNowDate());
        }
    }
}
